﻿<%@ WebHandler Language="C#" Class="control" %>

using System;
using System.Web;
using System.Collections.Generic;

public class control : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        HttpRequest request = context.Request;
        AccountEnum type = (AccountEnum)Convert.ToInt32(context.Request.QueryString["type"]);
        string account = request.QueryString["account"];
        string code = request.QueryString["password"];
        int access = request.QueryString["access"] != null ? int.Parse(context.Request.QueryString["access"]) : 0;
        int id = request.QueryString["id"] != null ? int.Parse(context.Request.QueryString["id"]) : 0;
        string blogAccount = request.QueryString["blogAccount"];
        UserProxy proxy = new UserProxy();
        object data = null;
        switch (type)
        {
            case AccountEnum.ADD:
                data = proxy.AddUser(account, code, access);
                break;
            case AccountEnum.DELETE:
                data = proxy.DeleteUser(id);
                break;
            case AccountEnum.SELECT:
                data = proxy.SelectUser(account);
                break;
            case AccountEnum.UPDATE:
                data = proxy.UpdateUser(id, account, code, access);
                break;
            case AccountEnum.LOGIN:
                data = proxy.SelectUser(account);
                if (data is UserData && (data as UserData).password != code)
                    data = "密码错误，请重新输入密码或联系管理员";
                break;
            case AccountEnum.UPDATE_BLOG_MAP:
                context.Response.Write(UpdateBlogMap(id, blogAccount));
                return;
        }
        string result;
        if (data == null)
            result = Message.CreateMessage(false, "");
        else if (data is UserData)
            result = Message.CreateMessage(true, (data as UserData).toJson());
        else
            result = Message.CreateMessage(false, data.ToString());
        context.Response.Write(result);
    }

    /// <summary>
    /// 更新和微博帐号的关联
    /// </summary>
    /// <param name="userId">用户标识</param>
    /// <param name="blogAccountStr">微博帐号列表，如果为空，则表示删除所有关联的微博，关联的帐号在数据库中以"帐号1,帐号2,帐号3"的形式保存</param>
    /// <returns></returns>
    private string UpdateBlogMap(int userId, string blogAccountStr)
    {
        DBUser db = new DBUser();
       db.DeleteBlogAccountMapByUserId(userId);
       string[] newBlogAccountArr = blogAccountStr.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
       if (newBlogAccountArr.Length > 0)
       {
           int[] intArr = Array.ConvertAll<string, int>(newBlogAccountArr, delegate(string s) { return int.Parse(s); });
           db.AddBlogAccountMap(userId, intArr);
       }
        return Message.CreateMessage(true, (new UserProxy().SelectUser(userId) as UserData).toJson());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}